<template>
  <PageContainer>
    <Header title="">
      <template #customTitle>
        <view
          class="w-full disFlxAC justify-between"
          :style="{
            height: 'inherit',
          }"
        >
          <text class="fz-18"></text>
          <view
            class="h-32 disFlxAC px-8 fz-12 br-20 mr-9 gap-4"
            style="background-color: #fff; color: #333333; border: 2rpx solid #ebedf0"
            @click="showSharePopup = true"
          >
            <text class="ali-icon">&#xe6da;</text>
            <text>分享</text>
          </view>
        </view>
      </template>
    </Header>
    <view class="pa-16">
      <view class="fz-24 default-color font-bold mb-12"><text>选择您要找的资源</text></view>
      <template v-for="(item, index) in routerList" :key="index">
        <view class="bg-white pa-16 disFlxAC br-8 mb-12" @click="handleToPage(item)">
          <TImage :src="item.icon" width="96rpx" height="96rpx" />
          <view class="default-color ml-10">
            <view class="fz-18 font-bold mb-4">
              <text>{{ item.title }}</text>
            </view>
            <view class="fz-12">
              <text>{{ item.subtitle }}</text>
            </view>
          </view>
        </view>
      </template>
    </view>
  </PageContainer>

  <CustomShare v-model:visible="showSharePopup" @register="handleCustomShareRegister" />
</template>

<script lang="ts" setup>
  /* resourceNavigation 资源导航 */
  import Header from '@/components/Header/index.vue';
  import PageContainer from '@/components/PageContainer/index.vue';
  import { Pages, type PagesType } from '@/router/types/pages';
  import TImage from '@/components/TImage/index.vue';
  import { ref } from 'vue';
  import { getUrl, navigateTo } from '@/router';
  import { toHuibangCloud } from '@/utils/dynamicConfig';
  import { Toast } from '@/utils';
  import { addAppShareLog, orgResourceConfigList } from '@/api';
  import type { ResourceConfigResult } from '@/api/indexTypes';
  import { onPageScroll, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app';
  import CustomShare from '@/components/CustomShare/index.vue';
  import { useAppShare } from '@/hooks/core/useAppShare';
  import { useCustomShare } from '@/components/CustomShare/hooks/useCustomShare';
  import type { CustomNavigateToOptions } from '@/router/types/nav';

  const routerList = ref<ResourceConfigResult[]>([]);
  orgResourceConfigList().then((res) => (routerList.value = res));

  const { setWxSharePath, createShareQuery, setWxShareTimelineQuery } = useAppShare();
  const showSharePopup = ref(false);
  const [
    customShareRegister,
    { setWxShareOptions, setShareLogId, setShareTimelineContent, setInternalLinkOptions },
  ] = useCustomShare({
    buttons: {
      share: true,
      poster: false,
      timeline: true,
      copyLink: true,
      insertOffiaccount: true,
      offiaccount: true,
      downloadMiniprogramCode: true,
    },
  });

  /** 设置分享参数 */
  async function handleCustomShareRegister(e: any) {
    customShareRegister(e);
    const query: CustomNavigateToOptions<'resourceNavigation'> = {
      name: 'resourceNavigation',
      options: {},
    };
    setInternalLinkOptions(query);
    const path = setWxSharePath(query);

    const routeQuery = createShareQuery({});
    setWxShareOptions([
      {
        title: '邀你一起找人脉、找供需、找产品…',
        imageUrl: 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/hz_bg.png',
        path,
      },
    ]);
    const { id: shareLogId } = await addAppShareLog({
      path: getUrl('resourceNavigation'),
      query: JSON.stringify(routeQuery),
    });
    setShareLogId(shareLogId);
    const shareTimelineContent = {
      query: setWxShareTimelineQuery(routeQuery),
      title: '邀你一起找人脉、找供需、找产品…',
      imageUrl: 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/hz_bg.png',
    };
    setShareTimelineContent(shareTimelineContent);
  }

  function handleToPage(item: ResourceConfigResult) {
    if (item.path === 'cloudHome') {
      toHuibangCloud('storeHome', { type: 'home' });
      return;
    }
    if (Pages[item.path as PagesType]) {
      navigateTo({ name: item.path as any });
      return;
    }

    Toast('暂无关联内容');
  }

  onPageScroll(() => {});
  onShareAppMessage(() => ({}));
  onShareTimeline(() => ({}));
</script>

<style lang="scss" scoped></style>
